﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace HITs
{
    public partial class Chart_Line : Form
    {
        float gia_thamchieu, dieuchinhdulieu, gia_truoc_dieu_chinh;
        const int soNgayHienThi = 150;
        string nameCP;
        CoPhieu cp_Temp;
        List<float> listGiaCP;
        List<float> listKhoiluongCP;
        List<string> listNgayGiaoDich;
        List<string> listCPTheoDoi;
        List<float> listHistogramMACD;
        List<float> listTongHopIMI_RSI_MFI = new List<float>();
        private List<float> listXanhVoDoLong;
        private List<float> listDoVoXanhLong;
        string message_str;

        public Chart_Line()
        {
            InitializeComponent();

            if (ShareData.danhsachCoPhieu != null && ShareData.danhsachCoPhieu.DanhSachCoPhieu_List.Count != 0)
            {
                cmbx_tenCP.Items.Clear();
                foreach (string item in ShareData.danhsachCoPhieu.DanhSachTenCoPhieu)
                    cmbx_tenCP.Items.Add(item);
                listXanhVoDoLong = new List<float>();
                listDoVoXanhLong = new List<float>();

              

                //cmbx_tenCP.SelectedIndex = ShareData.danhsachCoPhieu.DanhSachTenCoPhieu.IndexOf("^VNINDEX");
            }
        }

        private void CalculateMACD_BollingerBand(string nameCP, List<float> listValues, List<float> listKhoiLuong, List<string> listNgayGiaoDich)
        {
            #region Tính toán dữ liệu
            message_str = "";
            List<float> listData = new List<float>();
            listData.Add(gia_thamchieu * (1 - (trackBar.Value * 0.5f) / 100f));
            foreach (float val in listValues)
                listData.Add(val);

            List<float> MA13 = Indicators.EMA(listData, 13);
            List<float> MA34 = Indicators.EMA(listData, 34);

            List<float> MACD = new List<float>();
            List<float> signalLine = new List<float>();
            List<float> listChechLech = new List<float>();

            if (rdbtt_HistogramGia.Checked)
                Indicators.MACD(listData, 12, 26, 9, ref MACD, ref signalLine);
            else
                Indicators.MACD(listTongHopIMI_RSI_MFI, 12, 26, 9, ref MACD, ref signalLine);

            for (int i = 0; i < MACD.Count && i < soNgayHienThi; i++)
                listChechLech.Add(MACD[i] - signalLine[i]);
            listHistogramMACD = new List<float>(listChechLech);
            List<float> listSignal_Buy = Indicators.GetSignalBuySell(listChechLech);

            int count_step = 0;
            float temp_value = listChechLech[0];
            List<float> list_TKChuky = new List<float>();
            for (int i = 0; i < listChechLech.Count; i++)
            {
                if ((temp_value < 0f && listChechLech[i] < 0)
                    || (temp_value > 0f && listChechLech[i] > 0))
                    count_step++;
                else
                {
                    list_TKChuky.Add(count_step);
                    count_step = 0;
                    temp_value = listChechLech[i];
                }
            }

            float max, min, average;
            CoreFunctions.Tk_ThongKeMoTa(list_TKChuky, out max, out min, out average);
            lb_TKChuky.Text = "TK sóng - max= " + max + " - min= " + min + " - avg= " + Math.Round(average, 0);
            lb_TKChuky.Text += " - std = " + Math.Round(CoreFunctions.Tk_SampleStandardDeviation(list_TKChuky), 0);

            List<float> BB_tren = new List<float>();
            List<float> BB_duoi = new List<float>();
            List<float> BB_giua = new List<float>();
            List<float> BB_temp = new List<float>();
            Indicators.BollingerBand(listData, 20, ref BB_giua, ref BB_tren, ref BB_duoi);
            for (int i = 0; i < BB_tren.Count; i++)
            {
                BB_temp.Add(0.25f * (BB_tren[i] - BB_duoi[i]) + BB_duoi[i]);
            }

            List<float> EMA_KhoiLuong_8 = new List<float>();
            List<float> EMA_KhoiLuong_21 = new List<float>();
            EMA_KhoiLuong_8 = Indicators.EMA(listKhoiLuong, 8);
            EMA_KhoiLuong_21 = Indicators.EMA(listKhoiLuong, 21);

            List<float> SharpeRatio30 = new List<float>();
            SharpeRatio30 = CoreFunctions.Tk_GetSharpeRatio(listData, 30);

            List<float> listSong1 = new List<float>();
            /// Xác định số đỉnh của sóng hiện thời
            for (int i = 0; i < listHistogramMACD.Count - 1; i++)
            {
                listSong1.Add(listHistogramMACD[i]);
                if ((listHistogramMACD[i] > 0 && listHistogramMACD[i + 1] < 0)
                    || (listHistogramMACD[i] < 0 && listHistogramMACD[i + 1] > 0))
                    break;
            }
            List<float> listGopHistogram = CoreFunctions.MR_GomGiaTri(listSong1, 0.05f);
            for (int i = 0; i < listGopHistogram.Count; i++)
                listGopHistogram[i] *= -1f;

            List<float> listDongTien = new List<float>();
            float sum = Indicators.TinhDongTienDuongAm(listValues, listKhoiLuong, listKhoiLuong.Count, ref listDongTien);

            //List<List<float>> listGia = cp_Temp.Xuly_DieuChinhGia();
            //List<float> KlingerOsillator = new List<float>();
            //List<float> signal_Klinger = new List<float>();

            //List<List<float>> listGia = cp_Temp.Xuly_DieuChinhGia();
            //Indicators.KlingerOscillator(CoreFunctions.Xuly_DaoNguocList(listGia[1]), CoreFunctions.Xuly_DaoNguocList(listGia[2])
            //    , CoreFunctions.Xuly_DaoNguocList(listGia[3]), CoreFunctions.Xuly_DaoNguocList(cp_Temp.DanhSachKhoiLuongGiaoDich_Int)
            //    , ref KlingerOsillator, ref signal_Klinger);

            //KlingerOsillator = CoreFunctions.Xuly_DaoNguocList(KlingerOsillator);
            //signal_Klinger = CoreFunctions.Xuly_DaoNguocList(signal_Klinger);

            //for (int i = 0; i < KlingerOsillator.Count; i++)
            //    KlingerOsillator[i] /= 100000;

            //for (int i = 0; i < signal_Klinger.Count; i++)
            //    signal_Klinger[i] /= 100000;
            #endregion

            #region Vẽ Chart
            this.Text = nameCP + "  -  MACD";

            #region chart_MACD - histogram MACD
            chart_MACD.Series.Clear();
            chart_MACD.ChartAreas[0].AxisX.Enabled = AxisEnabled.False;

            chart_MACD.Series.Add("Histogram MACD");
            chart_MACD.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
            chart_MACD.Series[0].Points.DataBindY(listChechLech);
            chart_MACD.Series[chart_MACD.Series.Count - 1].YAxisType = AxisType.Secondary;

           
            chart_MACD.Series.Add("Signal");
            chart_MACD.Series[chart_MACD.Series.Count - 1].ChartType = SeriesChartType.Column;
            chart_MACD.Series[chart_MACD.Series.Count - 1].Points.DataBindY(listSignal_Buy);
            chart_MACD.Series[chart_MACD.Series.Count - 1].YAxisType = AxisType.Secondary;

            chart_MACD.Series.Add("IMI_RSI_MFI");
            chart_MACD.Series[chart_MACD.Series.Count - 1].ChartType = SeriesChartType.Spline;
            chart_MACD.Series[chart_MACD.Series.Count - 1].BorderWidth = 3;
            chart_MACD.Series[chart_MACD.Series.Count - 1].Color = Color.YellowGreen;
            chart_MACD.Series[chart_MACD.Series.Count - 1].Points.DataBindY(
                listTongHopIMI_RSI_MFI.GetRange(int.Parse(txtbx_songaycach.Text),soNgayHienThi));
                //CoreFunctions.Xuly_LayNphantudautien(listTongHopIMI_RSI_MFI, soNgayHienThi));
            chart_MACD.Series[chart_MACD.Series.Count - 1].YAxisType = AxisType.Primary;
            
            chart_MACD.Series.Add("Gop Histogram");
            chart_MACD.Series[chart_MACD.Series.Count - 1].ChartType = SeriesChartType.Column;
            chart_MACD.Series[chart_MACD.Series.Count - 1].Points.DataBindY(listGopHistogram);
            chart_MACD.Series[chart_MACD.Series.Count - 1].YAxisType = AxisType.Secondary;

            //chart_MACD.Series.Add("IMI");
            //chart_MACD.Series[chart_MACD.Series.Count - 1].ChartType = SeriesChartType.Spline;
            //chart_MACD.Series[chart_MACD.Series.Count - 1].BorderWidth = 3;
            //chart_MACD.Series[chart_MACD.Series.Count - 1].Color = Color.YellowGreen;
            //chart_MACD.Series[chart_MACD.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(listIMI, soNgayHienThi));
            //chart_MACD.Series[chart_MACD.Series.Count - 1].YAxisType = AxisType.Secondary;

            //chart_MACD.Series.Add("RSI");
            //chart_MACD.Series[chart_MACD.Series.Count - 1].ChartType = SeriesChartType.Spline;
            //chart_MACD.Series[chart_MACD.Series.Count - 1].BorderWidth = 3;
            //chart_MACD.Series[chart_MACD.Series.Count - 1].Color = Color.Red;
            //chart_MACD.Series[chart_MACD.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(listRSI, soNgayHienThi));
            //chart_MACD.Series[chart_MACD.Series.Count - 1].YAxisType = AxisType.Secondary;

            //chart_MACD.Series.Add("MFI");
            //chart_MACD.Series[chart_MACD.Series.Count - 1].ChartType = SeriesChartType.Spline;
            //chart_MACD.Series[chart_MACD.Series.Count - 1].BorderWidth = 3;
            //chart_MACD.Series[chart_MACD.Series.Count - 1].Color = Color.Gold;
            //chart_MACD.Series[chart_MACD.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(listMFI, soNgayHienThi));
            //chart_MACD.Series[chart_MACD.Series.Count - 1].YAxisType = AxisType.Secondary;

            //chart_MACD.Series.Add("SharpeRatio30");
            //chart_MACD.Series[chart_MACD.Series.Count - 1].ChartType = SeriesChartType.Spline;
            //chart_MACD.Series[chart_MACD.Series.Count - 1].BorderWidth = 3;
            //chart_MACD.Series[chart_MACD.Series.Count - 1].Color = Color.YellowGreen;
            //chart_MACD.Series[chart_MACD.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(SharpeRatio30, soNgayHienThi));
            //chart_MACD.Series[chart_MACD.Series.Count - 1].YAxisType = AxisType.Secondary;

            //List<float> listZero = new List<float>();
            //for (int i = 0; i < soNgayHienThi; i++)
            //    listZero.Add(0);
            //chart_MACD.Series.Add("SharpeRatioZero");
            //chart_MACD.Series[chart_MACD.Series.Count - 1].ChartType = SeriesChartType.Spline;
            //chart_MACD.Series[chart_MACD.Series.Count - 1].BorderWidth = 1;
            //chart_MACD.Series[chart_MACD.Series.Count - 1].Color = Color.Red;
            //chart_MACD.Series[chart_MACD.Series.Count - 1].Points.DataBindY(listZero);
            //chart_MACD.Series[chart_MACD.Series.Count - 1].YAxisType = AxisType.Secondary;

            //chart_MACD.Series.Add("KlingerOsc 1M");
            //chart_MACD.Series[chart_MACD.Series.Count - 1].ChartType = SeriesChartType.Spline;
            //chart_MACD.Series[chart_MACD.Series.Count - 1].BorderWidth = 2;
            //chart_MACD.Series[chart_MACD.Series.Count - 1].Color = Color.YellowGreen;
            //chart_MACD.Series[chart_MACD.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(KlingerOsillator, soNgayHienThi));
            //chart_MACD.Series[chart_MACD.Series.Count - 1].YAxisType = AxisType.Secondary;

            //chart_MACD.Series.Add("signalKlinger 1M");
            //chart_MACD.Series[chart_MACD.Series.Count - 1].ChartType = SeriesChartType.Spline;
            //chart_MACD.Series[chart_MACD.Series.Count - 1].BorderWidth = 2;
            //chart_MACD.Series[chart_MACD.Series.Count - 1].Color = Color.YellowGreen;
            //chart_MACD.Series[chart_MACD.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(signal_Klinger, soNgayHienThi));
            //chart_MACD.Series[chart_MACD.Series.Count - 1].YAxisType = AxisType.Secondary;
            //chart_MACD.Series[chart_MACD.Series.Count - 1].BorderDashStyle = ChartDashStyle.DashDotDot;

           
            #endregion

            #region Chart_giá - MACD - Bollinger Band
            chart_gia.Series.Clear();

            chart_gia.ChartAreas[0].AxisX.Enabled = AxisEnabled.False;
            chart_gia.ChartAreas[0].AxisY2.Enabled = AxisEnabled.False;
            chart_gia.ChartAreas[0].AxisY.LineColor = Color.Silver;
            chart_gia.ChartAreas[0].AxisY.LineDashStyle = ChartDashStyle.Dot;

            chart_gia.ChartAreas[0].AxisY2.LineColor = Color.Silver;
            chart_gia.ChartAreas[0].AxisY2.LineDashStyle = ChartDashStyle.Dot;

            chart_gia.ChartAreas[0].BorderDashStyle = ChartDashStyle.DashDotDot;
            chart_gia.ChartAreas[0].IsSameFontSizeForAllAxes = true;

            chart_gia.Series.Add("Giá");
            chart_gia.Series[chart_gia.Series.Count - 1].ChartType = SeriesChartType.Spline;
            chart_gia.Series[chart_gia.Series.Count - 1].BorderWidth = 3;
            chart_gia.Series[chart_gia.Series.Count - 1].Color = Color.Blue;
            chart_gia.Series[chart_gia.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(listData, soNgayHienThi));
            chart_gia.Series[chart_gia.Series.Count - 1].LabelBorderDashStyle = ChartDashStyle.DashDotDot;
                       
            chart_gia.Series[chart_gia.Series.Count - 1].LabelBorderDashStyle = ChartDashStyle.DashDotDot;
            chart_gia.Series.Add("MACD");
            chart_gia.Series[chart_gia.Series.Count - 1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
            chart_gia.Series[chart_gia.Series.Count - 1].BorderWidth = 2;
            chart_gia.Series[chart_gia.Series.Count - 1].Color = Color.GreenYellow;
            chart_gia.Series[chart_gia.Series.Count - 1].YAxisType = AxisType.Secondary;
            chart_gia.Series[chart_gia.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(MACD, soNgayHienThi));

            chart_gia.Series.Add("Tin Hieu");
            chart_gia.Series[chart_gia.Series.Count - 1].ChartType = SeriesChartType.RangeColumn;           
            chart_gia.Series[chart_gia.Series.Count - 1]["PixelPointWidth"] = "2";
            chart_gia.Series[chart_gia.Series.Count - 1].Color = Color.Gold;

            for (int i = 0; i < listSignal_Buy.Count; i++)
            {
                if (listSignal_Buy[i] > 0)
                    chart_gia.Series[chart_gia.Series.Count - 1].Points.AddXY(i,
                         listData[i], listData[i] + (listData[i] * 0.05f));
                else if (listSignal_Buy[i] < 0)
                    chart_gia.Series[chart_gia.Series.Count - 1].Points.AddXY(i,
                         listData[i] - (listData[i] * 0.05f), listData[i]);                
            }

            //chart_gia.Series.Add("Tin Hieu");
            //chart_gia.Series[chart_gia.Series.Count - 1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
            //chart_gia.Series[chart_gia.Series.Count - 1].BorderWidth = 2;
            //chart_gia.Series[chart_gia.Series.Count - 1].Color = Color.Gold;
            //chart_gia.Series[chart_gia.Series.Count - 1].YAxisType = AxisType.Secondary;
            //chart_gia.Series[chart_gia.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(listValue_TinHieu, soNgayHienThi));

            chart_gia.Series.Add("Signal Line");
            chart_gia.Series[chart_gia.Series.Count - 1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
            chart_gia.Series[chart_gia.Series.Count - 1].BorderWidth = 2;
            chart_gia.Series[chart_gia.Series.Count - 1].Color = Color.GreenYellow;
            chart_gia.Series[chart_gia.Series.Count - 1].BorderDashStyle = ChartDashStyle.DashDotDot;
            chart_gia.Series[chart_gia.Series.Count - 1].YAxisType = AxisType.Secondary;
            chart_gia.Series[chart_gia.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(signalLine, soNgayHienThi));

            chart_gia.Series.Add("BB Trên");
            chart_gia.Series[chart_gia.Series.Count - 1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
            chart_gia.Series[chart_gia.Series.Count - 1].BorderWidth = 1;
            chart_gia.Series[chart_gia.Series.Count - 1].Color = Color.DimGray;
            chart_gia.Series[chart_gia.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(BB_tren, soNgayHienThi));

            chart_gia.Series.Add("BB Dưới");
            chart_gia.Series[chart_gia.Series.Count - 1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
            chart_gia.Series[chart_gia.Series.Count - 1].BorderWidth = 1;
            chart_gia.Series[chart_gia.Series.Count - 1].Color = Color.DimGray;
            chart_gia.Series[chart_gia.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(BB_duoi, soNgayHienThi));

            chart_gia.Series.Add("BB 1/4");
            chart_gia.Series[chart_gia.Series.Count - 1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
            chart_gia.Series[chart_gia.Series.Count - 1].BorderWidth = 1;
            chart_gia.Series[chart_gia.Series.Count - 1].Color = Color.DimGray;
            chart_gia.Series[chart_gia.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(BB_temp, soNgayHienThi));

            chart_gia.Series.Add("MA 13");
            chart_gia.Series[chart_gia.Series.Count - 1].ChartType = SeriesChartType.Spline;
            chart_gia.Series[chart_gia.Series.Count - 1].BorderWidth = 2;
            chart_gia.Series[chart_gia.Series.Count - 1].Color = Color.Snow;
            chart_gia.Series[chart_gia.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(MA13, soNgayHienThi));
            chart_gia.Series[chart_gia.Series.Count - 1].LabelBorderDashStyle = ChartDashStyle.DashDotDot;

            chart_gia.Series.Add("MA 34");
            chart_gia.Series[chart_gia.Series.Count - 1].ChartType = SeriesChartType.Spline;
            chart_gia.Series[chart_gia.Series.Count - 1].BorderWidth = 2;
            chart_gia.Series[chart_gia.Series.Count - 1].Color = Color.Red;
            chart_gia.Series[chart_gia.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(MA34, soNgayHienThi));
                     
            //if (nameCP == "^VNINDEX" )
            //{                
            //    if(listXanhVoDoLong.Count == 0 && listDoVoXanhLong.Count == 0)
            //        ShareData.danhsachCoPhieu.Testing_HienTuong_XanhVoDoLong(ref listXanhVoDoLong, ref listDoVoXanhLong, 0.7f);
            //    int indexVNIndex = 0;
            //    foreach (CoPhieu cp in ShareData.danhsachCoPhieu.DanhSachCoPhieu_List)                
            //        if (cp.TenCoPhieu == nameCP)
            //        {
            //            indexVNIndex = cp.DanhSachNgayGiaoDich_String.IndexOf(listNgayGiaoDich[0]);
            //        }
            //    List<float> listTempXanhVoDoLong = new List<float>();
            //    List<float> listTempDoVoXanhLong = new List<float>();
            //    for (int i = indexVNIndex; i < listXanhVoDoLong.Count; i++)
            //    {
            //        listTempDoVoXanhLong.Add(listDoVoXanhLong[i]);
            //        listTempXanhVoDoLong.Add(listXanhVoDoLong[i]);
            //    }
            //    chart_gia.Series.Add("Xanh Vỏ Đỏ Lòng");
            //    chart_gia.Series[chart_gia.Series.Count - 1].ChartType = SeriesChartType.Spline;
            //    chart_gia.Series[chart_gia.Series.Count - 1].BorderWidth = 1;            
            //    chart_gia.Series[chart_gia.Series.Count - 1].Points.DataBindY
            //        (CoreFunctions.Xuly_LayNphantudautien(listTempXanhVoDoLong, soNgayHienThi));

            //    chart_gia.Series.Add("Đỏ Vỏ Xanh Lòng");
            //    chart_gia.Series[chart_gia.Series.Count - 1].ChartType = SeriesChartType.Spline;
            //    chart_gia.Series[chart_gia.Series.Count - 1].BorderWidth = 1;
            //    chart_gia.Series[chart_gia.Series.Count - 1].Points.DataBindY
            //        (CoreFunctions.Xuly_LayNphantudautien(listTempDoVoXanhLong, soNgayHienThi));
            //}
           
            #endregion

            #region Chart_khoiluong
            chart_khoiluong.Series.Clear();
            chart_khoiluong.ChartAreas[0].AxisX.Enabled = AxisEnabled.False;
            
            //chart_khoiluong.Series.Add("Dòng tiền");
            //chart_khoiluong.Series[chart_khoiluong.Series.Count - 1].Color = Color.Red;
            //chart_khoiluong.Series[chart_khoiluong.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(listDongTien, soNgayHienThi));


            chart_khoiluong.Series.Add("Khối lượng 10K");
            chart_khoiluong.Series[chart_khoiluong.Series.Count - 1].Color = Color.Red;
            chart_khoiluong.Series[chart_khoiluong.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(listKhoiLuong, soNgayHienThi));

            chart_khoiluong.Series.Add("EMA 8");
            chart_khoiluong.Series[chart_khoiluong.Series.Count - 1].ChartType = SeriesChartType.Spline;
            chart_khoiluong.Series[chart_khoiluong.Series.Count - 1].BorderWidth = 2;
            chart_khoiluong.Series[chart_khoiluong.Series.Count - 1].Color = Color.Blue;
            chart_khoiluong.Series[chart_khoiluong.Series.Count - 1].BorderDashStyle = ChartDashStyle.Dot;
            chart_khoiluong.Series[chart_khoiluong.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(EMA_KhoiLuong_8, soNgayHienThi));

            chart_khoiluong.Series.Add("EMA 21");
            chart_khoiluong.Series[chart_khoiluong.Series.Count - 1].ChartType = SeriesChartType.Spline;
            chart_khoiluong.Series[chart_khoiluong.Series.Count - 1].BorderWidth = 2;
            chart_khoiluong.Series[chart_khoiluong.Series.Count - 1].Color = Color.YellowGreen;
            chart_khoiluong.Series[chart_khoiluong.Series.Count - 1].BorderDashStyle = ChartDashStyle.Dot;
            chart_khoiluong.Series[chart_khoiluong.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(EMA_KhoiLuong_21, soNgayHienThi));
            #endregion

            #endregion
        }

        #region Function Window
      
        private void cmbx_tenCP_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbx_tenCP.SelectedItem != null)
            {
                foreach (CoPhieu cp in ShareData.danhsachCoPhieu.DanhSachCoPhieu_List)
                    if (cp.TenCoPhieu == cmbx_tenCP.Text)
                    {
                        cp_Temp = cp;
                        nameCP = cp.TenCoPhieu;
                        List<float> listValues = new List<float>();
                        if (ckbx_dieuchinhdulieu.Checked == false)
                            listValues = cp.DanhSachGiaDongCua;
                        else
                            listValues = cp.DanhSachGiaDongCua_DieuChinh;

                        listGiaCP = new List<float>();
                        listKhoiluongCP = new List<float>();
                        gia_truoc_dieu_chinh= 0.0f;
                        listNgayGiaoDich = new List<string>();
                        for (int i = int.Parse(txtbx_songaycach.Text); i < listValues.Count; i++)
                        {
                            if (gia_truoc_dieu_chinh == 0.0f)
                                gia_truoc_dieu_chinh = cp.DanhSachGiaDongCua[i];

                            listGiaCP.Add(listValues[i]);
                            listKhoiluongCP.Add(cp.DanhSachKhoiLuongGiaoDich[i] / 10000);
                            listNgayGiaoDich.Add(cp.DanhSachNgayGiaoDich_String[i]);
                          
                        }

                        gia_thamchieu = listGiaCP[0];

                        txtbx_gia.Text = gia_truoc_dieu_chinh.ToString();

                        listTongHopIMI_RSI_MFI = new List<float>();
                        List<float> listIMI = new List<float>();
                        listIMI = Indicators.IMI(14, cp_Temp.DanhSachGiaMoCua, cp_Temp.DanhSachGiaDongCua);

                        List<float> listRSI = new List<float>();
                        listRSI = Indicators.RSI(20, cp_Temp.DanhSachGiaDongCua);

                        List<float> listMFI = new List<float>();
                        listMFI = Indicators.MFI(14, cp_Temp.DanhSachGiaCaoNhat, cp_Temp.DanhSachGiaThapNhat,
                            cp_Temp.DanhSachGiaDongCua, cp_Temp.DanhSachKhoiLuongGiaoDich_Int);

                        for (int i = 0; i < listIMI.Count && i < listRSI.Count && i < listMFI.Count; i++)
                            listTongHopIMI_RSI_MFI.Add((listIMI[i] + listRSI[i] + listMFI[i]) / 3);
                        listTongHopIMI_RSI_MFI = Indicators.SMA(listTongHopIMI_RSI_MFI, 3);

                        CalculateMACD_BollingerBand(nameCP, listGiaCP, listKhoiluongCP, listNgayGiaoDich);
                        lb_MFI.Text = "MFI14: " + cp.TinhDanhSachChiSo_MoneyFlowIndex_MFI(14, int.Parse(txtbx_songaycach.Text)).ToString();
                        lb_RSI.Text = "RSI20: " + cp.TinhDanhSachChiSo_RelativeStrengIndex_RSI(20, int.Parse(txtbx_songaycach.Text)).ToString();
                        lb_IMI07.Text = "IMI14: " + cp.TinhDanhSachChiSo_IntradayMomentumIndex_IMI(14, int.Parse(txtbx_songaycach.Text)).ToString();
                        trackBar.Value = 0;
                        break;
                    }
            }
        }

        private void trackBar_Scroll(object sender, EventArgs e)
        {
            ///gia_thamchieu = gia_thamchieu * (1 - (trackBar.Value * 0.5f) / 100f);

            float val = gia_truoc_dieu_chinh * (1 - (trackBar.Value * 0.5f) / 100f);
            val = (float)Math.Round(val, 1);
            txtbx_gia.Text = val.ToString();
            string Mess = "";            
            CoreFunctions.MR_TinhDaoDongGia(listGiaCP, val, ref Mess);
            lb_MessDaoDongGia.Text = Mess;

            CalculateMACD_BollingerBand(nameCP, listGiaCP, listKhoiluongCP, listNgayGiaoDich);
        }

        private void txtbx_songaycach_TextChanged(object sender, EventArgs e)
        {
            foreach (char c in txtbx_songaycach.Text)
                if (char.IsDigit(c) == false)
                {
                    MessageBox.Show("Nhập số", "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtbx_songaycach.Text = "0";
                    break;
                }
            if (txtbx_songaycach.Text != "")
            {
                cmbx_tenCP_SelectedIndexChanged(null, null);
            }
        }

        private void btt_up_Click(object sender, EventArgs e)
        {
            bool flag = false;
            foreach (char c in txtbx_songaycach.Text)
                if (char.IsDigit(c) == false)
                {
                    flag = true;
                    break;
                }
            if (flag == false)
            {
                int val = int.Parse(txtbx_songaycach.Text);
                val -= 10;
                if (val < 0) val = 0;
                txtbx_songaycach.Text = val.ToString();
            }
        }

        private void btt_down_Click(object sender, EventArgs e)
        {
            bool flag = false;
            foreach (char c in txtbx_songaycach.Text)
                if (char.IsDigit(c) == false)
                {
                    flag = true;
                    break;
                }
            if (flag == false)
            {
                int val = int.Parse(txtbx_songaycach.Text);
                val += 10;
                if (val < 0) val = 0;
                txtbx_songaycach.Text = val.ToString();
            }
        }

        private void ckbx_dieuchinhdulieu_CheckedChanged(object sender, EventArgs e)
        {
            txtbx_dieuchinhdulieu.Enabled = ckbx_dieuchinhdulieu.Checked;
            if (ckbx_dieuchinhdulieu.Checked == true)
            {
                string[] part = txtbx_dieuchinhdulieu.Text.Split(new string[] { ">", "%" }, StringSplitOptions.RemoveEmptyEntries);
                dieuchinhdulieu = float.Parse(part[0]) / 100;
                //listGiaCP = cp_Temp.DanhSachGiaDongCua_DieuChinh;
            }
            else
            {
                dieuchinhdulieu = -10f;
                //listGiaCP = cp_Temp.DanhSachGiaDongCua;
            }
            cmbx_tenCP_SelectedIndexChanged(null, null);
            trackBar_Scroll(null, null);
        }

        private void Chart_Line_Load(object sender, EventArgs e)
        {
            listCPTheoDoi = CoreFunctions.File_LoadAnyFile(System.Windows.Forms.Application.StartupPath + @"\Data\CPTheoDoi.txt");
            cmbx_CPtheodoi.Items.Clear();
            foreach (string cp in listCPTheoDoi)
                if (cp != "")
                    cmbx_CPtheodoi.Items.Add(cp);
            if (listCPTheoDoi.Count != 0)
                cmbx_CPtheodoi.SelectedIndex = 0;
        }
        #endregion

        private void cmbx_CPtheodoi_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbx_tenCP.Text = cmbx_CPtheodoi.Text;
        }

        private void btt_Thongtin_Click(object sender, EventArgs e)
        {            
            message_str = CoreFunctions.Xuly_LayThongTin_HistogramMACD(listHistogramMACD);
            MessageBox.Show(message_str, "Thông Tin", MessageBoxButtons.OK);
        }

        private void rdbtt_HistogramGia_CheckedChanged(object sender, EventArgs e)
        {
            cmbx_tenCP_SelectedIndexChanged(null, null);
        }

        private void rdbtt_Histogram_AVG_RSI_MFI_IMI_CheckedChanged(object sender, EventArgs e)
        {
            cmbx_tenCP_SelectedIndexChanged(null, null);
        }

        private void btt_TK_Histogram_Click(object sender, EventArgs e)
        {
            ShareData.danhsachCoPhieu.Testing_MACD_Statistic_TinHieuMua(9, 1, 4, 5
                , ckbx_dieuchinhdulieu.Checked, cmbx_tenCP.SelectedItem.ToString());
        }

   
    }
}
